package jdbc;
import java.awt.Component;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.List;
import java.util.Vector;

import javax.swing.DefaultCellEditor;
import javax.swing.JButton;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableCellRenderer;
import javax.swing.table.TableColumn;

import jdbc.icon.DAO;

public class UserList extends JScrollPane {
	private static String sid ;
	String[] titles = { "序号", "纳税人姓名", "性别", "年龄", "身份证号码","单位名称","纳税金额","操作"};
	Object[][] tableData = null  ;
	public UserList() {
		super();
		
  	  try {
			
			Connection conn=DAO.getConn();
			PreparedStatement ps=conn.prepareStatement("select * from 纳税人管理 ");
			ResultSet rs=ps.executeQuery();
			int  m =0;
			while(rs.next()){
				m++;
			}
			tableData = new String[m][8];
			rs=ps.executeQuery();
			for (int i = 0;  rs.next(); i++) {
				for (int l = 0; l < 8; l++) {
					if(l>=7) {
						tableData[i][l]= "";
					}else {
						tableData[i][l] = rs.getString(l+1);

					}
				}
				
			}

		} catch (Exception e) {
			// TODO: handle exception
			JOptionPane.showMessageDialog(null, "查询失败");
			e.printStackTrace();
		}
    
   

		JTable table = new JTable(tableData, titles);
//		
//		
//		TableColumn editCol = table.getColumnModel().getColumn(titles.length-2);
//		
//		editCol.setCellRenderer(new TableCellRenderer() {
//			
//			@Override
//			public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus,
//					int row, int column) {
//				
//				return new JButton("修改");
//			}
//		});
		
		
//		editCol.setCellEditor(new TableCellEditor());
		
		TableColumn delCol = table.getColumnModel().getColumn(titles.length-1);
		
		delCol.setCellRenderer(new TableCellRenderer() {
			
			@Override
			public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus,
					int row, int column) {
				
				return new JButton("删除");
			}
		});
		
		
		delCol.setCellEditor(new TableDeleteCellEditor());
		getViewport().add(table);
	}
	public static void main(String[] args) {
		new UserList();
	}

	
	class TableCellEditor extends DefaultCellEditor{

		public TableCellEditor() {
			super(new JTextField());
			setClickCountToStart(1);
			
		}

		@Override
		public Component getTableCellEditorComponent(JTable table, Object value, boolean isSelected, int row,
				int column) {
			
			JButton btnButton = new JButton("修改");
			btnButton.addActionListener(new ActionListener() {
				
				@Override
				public void actionPerformed(ActionEvent e) {
					OrderEditDialog editOrderDialog = new OrderEditDialog();
					editOrderDialog.setTitle("修改");
					
					editOrderDialog.setBounds(100,100, 400, 260);
					editOrderDialog.setVisible(true);
					
					
					
					
				}
			});
			
			return btnButton;
		}
		
		
		
	}
	
	
	class TableDeleteCellEditor extends DefaultCellEditor{

		public TableDeleteCellEditor() {
			super(new JTextField());
			setClickCountToStart(1);
			
		}

		@Override
		public Component getTableCellEditorComponent(JTable table, Object value, boolean isSelected, int row,
				int column) {
			
			JButton btnButton = new JButton("删除");
			btnButton.addActionListener(new ActionListener() {
				
				@Override
				public void actionPerformed(ActionEvent e) {
					JOptionPane.showMessageDialog(null, "删除?");
					System.out.println("执行删除操作");
					try {
						System.out.println(table.getValueAt(row, 0)+"----");
						Connection conn=DAO.getConn();
						String sql = "DELETE FROM taxpayer_manage WHERE id = "+table.getValueAt(row, 0);
						PreparedStatement m =conn.prepareStatement(sql);
						if (m.executeUpdate() > 0) {
							System.out.println(table.getValueAt(row, 0)+"----");
						}
					} catch (Exception en) {
						// TODO Auto-generated catch block
						en.printStackTrace();
					} 
					WorkPanel.setRightComponet(new UserList());//刷新下右侧的 工作区
					
					
				}
			});
			
			return btnButton;
		}
		
	}
	
	
	
	
	
	class OrderListTableModel extends DefaultTableModel{

		List<Order> data ;
		
		
		public OrderListTableModel(List<Order> paramData) {
			super();
			this.data = paramData;
		}

		@Override
		public int getRowCount() {
			if(data==null || data.size() ==0) {
				return 0;
			}
			return data.size();
		}

		@Override
		public int getColumnCount() {
			return titles.length;
		}

		@Override
		public String getColumnName(int column) {
			// TODO Auto-generated method stub
			return titles[column];
		}

		@Override
		public Object getValueAt(int row, int column) {
			Order order = data.get(row);
//			String[] titles = { "账簿号", "归属账户", "总金额", "支付方式", "状态","创建时间" };

			switch (column) {
			case 0:
				return order.getOrderId();
			case 1:
				return order.getOrderAccount();
			case 2:
				return order.getOrderAmount();
			case 3:
				return order.getPayment();
			case 4:
				return order.getStatus();
			case 5:
				return order.getCreateTime()!=null?new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(order.getCreateTime()):"";
		
			}
			
			return "";
		}

		@Override
		public void setValueAt(Object aValue, int row, int column) {
			
		}
		
		
		
		
		
	}

	

}